package com.enniu.alipay.api.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

import com.enniu.alipay.api.data.util.DataSourceSwitcher;
import com.enniu.alipay.api.domain.SearchUser;
import com.enniu.alipay.api.domain.User;
import com.enniu.alipay.api.service.UserService;

@RestController
@RequestMapping("api")
public class BillController extends BaseController{
	
	@Autowired
	private UserService userService;
	
	@RequestMapping(value = "/page/{id}", method = RequestMethod.GET)
	@ResponseStatus(HttpStatus.OK)
	public User getBillByBillId(@PathVariable String id)
	{
		if("ERROR".equals(id))
		{
			//验证restful 可以用hibernate validate来处理
			 throw new RuntimeException();
		}
		Integer userId = Integer.valueOf(id);
		return userService.findUserById(userId);
	}
	
	@RequestMapping(value = "/testquery", method = RequestMethod.GET)
	@ResponseStatus(HttpStatus.OK)
	public Page<User> testJpaQuery()
	{
		SearchUser searchUser = new SearchUser();
		searchUser.setId(1);
		searchUser.setPage(1);
		searchUser.setLimit(10);
		searchUser.setUsername("张三");
		DataSourceSwitcher.set("dataSource1");
		Page<User> result = userService.findAllUser(searchUser);
		return result;
	}
	
	
	@RequestMapping(value = "/testem", method = RequestMethod.GET)
	@ResponseStatus(HttpStatus.OK)
	public List<User> testEM()
	{
		User user = new User();
		user.setId(1);
		List<User> users = userService.findByUser(user);
		return users;
	}
	
	@RequestMapping(value = "/sql", method = RequestMethod.GET)
	@ResponseStatus(HttpStatus.OK)
	public List<User> testsql()
	{
		User user = new User();
		user.setId(1);
		List<User> users = userService.findBySql(user);
		return users;
	}
	

}
